//
//  JQMineProfileVC.m
//  JQLive
//
//  Created by Bruce on 2020/8/18.
//  Copyright © 2020 mlkq. All rights reserved.
//

#import "JQMineProfileVC.h"
#import "UIView+Extension.h"
#import "JQMineProfileHeadCell.h"
#import "JQMineProfileCell.h"
#import "JQActionCell.h"
#import "JQGraphicVerificationView.h"
#import "JQMineProfileSignatureVC.h"

@interface JQMineProfileVC ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *nameArr;
@property (nonatomic, strong) UITableView *actionTableView;
@property (nonatomic, copy) NSArray *actionArr;
@property (nonatomic, strong) UIView *actionBgView;

@property (nonatomic, strong) UITextField *graphicVerificationTextField;// 图形验证输入框
@property (nonatomic, strong) UIButton *graphicVerificationEnterBtn;// 图形验证 确认按钮
@property (nonatomic, strong) UIImageView *graphicVerificationImageV;// 图形验证图片
@property (nonatomic, strong) CAGradientLayer *graphicVerificationGl;

@end

@implementation JQMineProfileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的资料";
    self.nameArr = @[@"头像", @"昵称", @"性别", @"个性签名"];
    self.actionArr = @[@"男士", @"女士", @"保密"];
    [self addTopView];
    [self addNavBar];
    [self addTableView];
}
- (void)addNavBar{
    MY_WEAK;
    UILabel *titleLb = [[UILabel alloc] init];
    [self.view addSubview:titleLb];
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        MY_STRONG;
        make.size.mas_equalTo(CGSizeMake(145, 25));
        make.centerX.mas_equalTo(strongSelf.view);
        make.top.mas_equalTo(strongSelf.view.mas_top).offset(44);
    }];
    titleLb.textColor = [UIColor whiteColor];
    titleLb.font = [UIFont systemFontOfSize:18];
    titleLb.text = self.title;
    titleLb.textAlignment = NSTextAlignmentCenter;
    
    
    // 返回
    UIButton *backBtn = [[UIButton alloc] init];
    [backBtn setImage:[UIImage svgImageNamed:@"nav_back" size:CGSizeMake(15, 15)] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        MY_STRONG;
        make.left.mas_equalTo(strongSelf.view.mas_left).offset(18);
        make.centerY.mas_equalTo(titleLb);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [backBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}
- (void)addTopView{
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 109);


    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,SCREEN_WIDTH,109);
    gl.startPoint = CGPointMake(1, 0.5);
    gl.endPoint = CGPointMake(0, 0.51);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:17/255.0 green:0/255.0 blue:169/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:9/255.0 green:37/255.0 blue:215/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [topView.layer addSublayer:gl];
    
//    [self.navigationController.navigationBar.layer addSublayer:gl];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

// tabview 加载
- (void)addTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 87, SCREEN_WIDTH, SCREEN_HEIGHT-87) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.backgroundColor = kColorWithHex(0xF2F4FC);
    [tableView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[JQMineProfileCell class] forCellReuseIdentifier:NSStringFromClass([JQMineProfileCell class])];
    [tableView registerClass:[JQMineProfileHeadCell class] forCellReuseIdentifier:NSStringFromClass([JQMineProfileHeadCell class])];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
}

#pragma mark - InitNAV
///// 设置导航栏的背景图，默认为 NavBarBackgroundImage
- (nullable UIImage *)navigationBarBackgroundImage{
    //    return [UIImage new];
    return [UIImage svgImageNamed:@"mine_background" size:CGSizeMake(SCREEN_WIDTH, 215)];
#pragma mark - 设置 导航栏的背景图  效果
    //     return [UIImage imageNamed:@"home_background"]; //这里的timg就是那张叶子图
}

/// 设置当前导航栏的 barTintColor，默认为 NavBarBarTintColor
- (nullable UIColor *)navigationBarBarTintColor{
    return [UIColor clearColor];
#pragma mark - 设置 导航栏的背景 颜色
    //    return MYHome_backgroundColor;
}
// 系统状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark - deleagte
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.actionTableView) {
        return 3;
    }
    if (section == 0) {
        return 4;
    }
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headV = [[UIView alloc] init];
    headV.backgroundColor = tableView.backgroundColor;
    return headV;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footV = [[UIView alloc] init];
    footV.backgroundColor = tableView.backgroundColor;
    return footV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.actionTableView) {
        return 54;
    }
    if (indexPath.row == 0){
        return 108;
    }
    return 48;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (tableView == self.actionTableView) {
// actionCll
        JQActionCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JQActionCell class]) forIndexPath:indexPath];
        [cell setDataWith:self.actionArr[indexPath.row]];
        return cell;
    }
    if (indexPath.section == 0 && indexPath.row == 0) {
        JQMineProfileHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JQMineProfileHeadCell class]) forIndexPath:indexPath];
        cell.backgroundColor = tableView.backgroundColor;
        [cell layoutIfNeeded];
        [cell.cellBackgroundV round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
////        [cell setCellImage:(nonnull UIImage *)];//获取image url赋值
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else{
        JQMineProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JQMineProfileCell class]) forIndexPath:indexPath];
        cell.backgroundColor = tableView.backgroundColor;
        [cell layoutIfNeeded];
        if ([tableView numberOfRowsInSection:indexPath.section]-1 == indexPath.row) {
//            [cell layoutIfNeeded];
            [cell.cellBackgroundV round:10 RectCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
        }
        if (indexPath.row == 1) {
            [cell setCellLb:self.nameArr[indexPath.row] cellDetailLb:@"起一个昵称特别短的名字"];
        }else{
            [cell setCellLb:self.nameArr[indexPath.row] cellDetailLb:nil];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    return [[UITableViewCell alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.actionTableView){
        // 选择啦 self.actionArr[indexPath.row]
        [self.actionBgView removeFromSuperview];
        [self.actionBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
        
    if (indexPath.row == 2) {
//        [self alterSexPortrait];
        [self addSelectAction];
    }
    // 测试 图形弹框
    if (indexPath.row == 1){
        // 使用下面方法 就可 加入验证码弹框页面 以及导出 输入图形验证码
        JQGraphicVerificationView *graphicVerificationV = [[JQGraphicVerificationView alloc] init];
        graphicVerificationV.imageUrl = nil;// 输入验证码url
        graphicVerificationV.block = ^(NSString * _Nullable graphicVerificationStr) {
            NSLog(@"输出验证码：%@", graphicVerificationStr);
        };
    }
    
    if (indexPath.row == 3){
        JQMineProfileSignatureVC *profileSignatureVC = [[JQMineProfileSignatureVC alloc] init];
        [self.navigationController pushViewController:profileSignatureVC animated:YES];
    }
}

// 自定义弹框 选择 性别
- (void)addSelectAction{
    UIView *actionBgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [[UIApplication sharedApplication].keyWindow addSubview:actionBgView];
    self.actionBgView = actionBgView;
    MY_WEAK;
    [self.actionBgView my_addTapGes:^{
        MY_STRONG;
        [strongSelf.actionBgView removeFromSuperview];
        [strongSelf.actionBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
    actionBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.289];
    
    UIView *whiteView = [[UIView alloc] init];
    [actionBgView addSubview:whiteView];
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(actionBgView);
        make.height.mas_equalTo(216+MY_BottomTabbarHeight);
    }];
    whiteView.backgroundColor = [UIColor whiteColor];
    [whiteView layoutIfNeeded];
    [whiteView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    
    UILabel *titleLb = [[UILabel alloc] init];
    [whiteView addSubview:titleLb];
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(whiteView.mas_top).offset(15);
        make.left.mas_equalTo(whiteView.mas_left).offset(125);
        make.right.mas_equalTo(whiteView.mas_right).offset(-125);
        make.height.mas_equalTo(20);
    }];
    titleLb.text = @"请选择你的性别";
    titleLb.textColor = MYColor9;
    titleLb.font = [UIFont systemFontOfSize:14];
    titleLb.textAlignment = NSTextAlignmentCenter;
    
    UITableView *actionTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [whiteView addSubview:actionTableView];
    self.actionTableView = actionTableView;
    [actionTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(whiteView.top).offset(54);
        make.left.right.bottom.equalTo(whiteView);
    }];
    actionTableView.delegate = self;
    actionTableView.dataSource = self;
    [actionTableView registerClass:[JQActionCell class] forCellReuseIdentifier:NSStringFromClass([JQActionCell class])];
    actionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark----- 选择性别弹出框
-(void)alterSexPortrait{
    /*  弹出提示框
     */
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择你的性别" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //按钮：男
    [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 点击 男
        NSLog(@"action：%@", action);
        
    }]];
    //按钮：女
    [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        // 女
        NSLog(@"action：%@", action);
        
        
    }]];
    //按钮：保密
    [alert addAction:[UIAlertAction actionWithTitle:@"保密" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        // 保密
        NSLog(@"action：%@", action);
        
        
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
//    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}
@end
